package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.*;
import com.woniuxy.oldmanapi.service.*;
import com.woniuxy.oldmanapi.vo.NumVo;
import com.woniuxy.oldmanapi.yvo.ElectricityVo;
import com.woniuxy.oldmanapi.yvo.OlderAndBedVo;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.woniuxy.oldmanapi.xvo.OlderVo;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
public class OlderController {
    @Autowired
    OlderService olderService;

    @Autowired
    CareService careService;
    @Autowired
    ContractService contractService;

    @Autowired
    LabelService labelService;
    //图表查询老人年龄分布
    //查询所有老人信息
    @GetMapping("/older/oldNum")
    public ResponseResult<List<NumVo>> oldNum(){
        ResponseResult<List<NumVo>> result = new ResponseResult<>();
        try {
            List<NumVo> numVo = olderService.oldNumByOld();
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(numVo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }
    //查询所有老人入住时间统计
    @GetMapping("/older/olderCheck/{year}")
    public ResponseResult<List<Integer>> olderCheck(@PathVariable("year") int year){
        ResponseResult<List<Integer>> result = new ResponseResult<>();
        try {
            List<Integer> nums = olderService.olderCheck(year);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(nums);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //查询所有老人信息
    @GetMapping("/older/selectOlderByCons")
    public ResponseResult<PageInfo<OlderVo>> selectOlderByCons(int now,int size,String name,
                                                               @RequestParam(defaultValue = "-1") int status,
                                                               @RequestParam(defaultValue = "0") int labelId,
                                                               @RequestParam(defaultValue = "0") int careId){
        ResponseResult<PageInfo<OlderVo>> result = new ResponseResult<>();
        try {
            PageInfo<OlderVo> pageInfo = olderService.selectOlderByCons(now,size,name,status,labelId,careId);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }
    //查询所有老人信息
    @GetMapping("/older/addEleSelectOlder/{buildingId}/{storeyId}/{roomId}")
    public ResponseResult<List<OlderAndBedVo>> addEleSelectOlder(@PathVariable("buildingId") int buildingId,
                                                                 @PathVariable( "storeyId") int storeyId,
                                                                 @PathVariable("roomId") int roomId){
        ResponseResult<List<OlderAndBedVo>> result = new ResponseResult<>();
        try {
            List<OlderAndBedVo> pageInfo = olderService.addEleSelectOlder(buildingId,storeyId,roomId);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //查询单个老人信息并回显
    @GetMapping("/older/selectOlderById")
    public ResponseResult<Older> selectOlderById(int id){
        ResponseResult<Older> result = new ResponseResult<>();
        try {
            Older older = olderService.selectOlderById(id);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(older);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;

    }

    //修改老人信息
    @PostMapping("/older/updateOlder")
    public  ResponseResult<Void> updateOlder(@RequestBody Older older){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            olderService.updateConsulter(older);
            result.setCode(200);
            result.setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //添加老人信息
    @PostMapping("/older/insertOlder")
    public ResponseResult<Void> insertOlder(@RequestBody Older older){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            olderService.insertOlder(older);
            result.setCode(200);
            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }



    //查询所有护理等级
    @GetMapping("/older/selectAllCare")
    public ResponseResult<List<Care>> selectAllCare(){
        ResponseResult<List<Care>> result = new ResponseResult<>();
        try {
            List<Care> care = careService.selectAllCare();
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(care);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;

    }

    //查询所有合同模板
    @GetMapping("/older/selectAllContract")
    public ResponseResult<List<Contract>> selectAllContract(){
        ResponseResult<List<Contract>> result = new ResponseResult<>();
        try {
            List<Contract> contracts = contractService.selectAllContract();
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(contracts);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;

    }

    //查询所有标签
    @GetMapping("/older/selectAllLabel")
    public ResponseResult<List<Label>> selectAllLabel(){
        ResponseResult<List<Label>> result = new ResponseResult<>();
        try {
            List<Label> labels = labelService.selectAllLabel();
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(labels);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;

    }

    /**
     *
     * 出院登记方法
     *
     */

    //查询status不为1 的 长者信息
    @GetMapping("/older/selectExitOlder")
    public ResponseResult<PageInfo<Older>> selectExitOlder(@RequestParam(defaultValue = "1") int now,@RequestParam(defaultValue = "3")int size,String name){
        ResponseResult<PageInfo<Older>> result = new ResponseResult<>();
        try {
            PageInfo<Older> pageInfo = olderService.selectOlderByExitStatus(now,size,name);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;

    }

    //查询status为1 的长者信息
    @GetMapping("/older/selectOlderByStatus")
    public ResponseResult<PageInfo<Older>> selectOlderByStatus(@RequestParam(defaultValue = "1")int now,@RequestParam(defaultValue = "3")int size,String name){
        ResponseResult<PageInfo<Older>> result = new ResponseResult<>();
        try {
            PageInfo<Older> pageInfo = olderService.selectOlderByStatus(now,size,name);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;

    }

    //老人登记退院，修改status为待退房（2）
    @GetMapping("/older/exitRegister")
    public ResponseResult<Void> exitRegister(int id, Date exitdate){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            olderService.updateOlderStatusById(id, exitdate);
            result.setCode(200);
            result.setMsg("登记成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }
    //确认退房修改status为退房（0）
    @GetMapping("/older/confirmExit")
    public ResponseResult<Void> confirmExit(int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            olderService.updateOlderStatusById(id);
            result.setCode(200);
            result.setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return  result;
    }

    //查询所有床位信息
    @GetMapping("/older/selectAllBed")
    public ResponseResult<List<Bed>> selectAllBed(){
        ResponseResult<List<Bed>> result = new ResponseResult<>();
        try {
            List<Bed> bedList = olderService.selectAllBed();
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(bedList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }
    @Autowired
    OrderService orderService;

    /**
     * 此方法为初始化外出申请表页面数据
     *
     * @param name 搜索框条件
     * @param now  分页 页数
     * @param size 分页 条数
     * @return
     */
    @GetMapping("/Older/list")
    public ResponseResult<PageInfo<OlderAndBedVo>> initSelectOlderAndBedByCons(String name, int now, int size) {
        ResponseResult<PageInfo<OlderAndBedVo>> result = new ResponseResult<>();
        try {
            PageInfo<OlderAndBedVo> pageInfo = orderService.initSelectOlderAndBedByCons(name, now, size);
            if (pageInfo.getList() == null) {
                result.setCode(204);
                result.setMsg("无满足条件对象");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
}
